package com.cskaoyan.config;


import com.cskaoyan.util.SessionUtil;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
       
        if (SessionUtil.getUserInfo() == null) {
            //没有登录时进行拦截

            //设置响应报文的编码格式
            response.setCharacterEncoding("UTF-8");
            //设置浏览器的编码格式
            response.setContentType("text/html;charset = utf-8");
            response.getWriter().println("{\"errno\":501,\"errmsg\":\"请登录\"}");
            return false;
        } else {
            return true;
        }

    }


}
